using RugbyClubWpf.Models;
using System.Collections.ObjectModel;
using RugbyClubWpf.Data;
using System.Linq;
using System.Windows.Input;
using RugbyClubWpf.Helpers;
using RugbyClubWpf.Views;
using System.Windows;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace RugbyClubWpf.ViewModels
{
    /// <summary>
    /// ViewModel for the Player view.
    /// </summary>
    public class PlayerViewModel : INotifyPropertyChanged
    {
        /// <summary>
        /// Gets or sets the collection of players.
        /// </summary>
        public ObservableCollection<Player> Players { get; set; }
        /// <summary>
        /// Gets or sets the collection of matches.
        /// </summary>
        public ObservableCollection<Match> Matches { get; set; }
        /// <summary>
        /// Gets or sets the collection of coaches.
        /// </summary>
        public ObservableCollection<Coach> Coaches { get; set; }
        /// <summary>
        /// Gets or sets the collection of announcements.
        /// </summary>
        public ObservableCollection<Announcement> Announcements { get; set; }

        /// <summary>
        /// Gets the command to add a player.
        /// </summary>
        public ICommand AddPlayerCommand { get; }
        /// <summary>
        /// Gets the command to delete a player.
        /// </summary>
        public ICommand DeletePlayerCommand { get; }
        /// <summary>
        /// Gets the command to update a player.
        /// </summary>
        public ICommand UpdatePlayerCommand { get; }

        private Player _selectedPlayer;
        /// <summary>
        /// Gets or sets the selected player.
        /// </summary>
        public Player SelectedPlayer
        {
            get => _selectedPlayer;
            set
            {
                _selectedPlayer = value;
                OnPropertyChanged();
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PlayerViewModel"/> class.
        /// </summary>
        /// <param name="userId">The user ID of the player.</param>
        public PlayerViewModel(int userId)
        {
            using (var context = new RugbyClubContext())
            {
                var player = context.Players.FirstOrDefault(p => p.UserId == userId);
                if (player != null)
                {
                    Players = new ObservableCollection<Player> { player };
                    var coach = context.Coaches.FirstOrDefault(c => c.CoachId == player.CoachId);
                    if (coach != null)
                    {
                        Coaches = new ObservableCollection<Coach> { coach };
                        Matches = new ObservableCollection<Match>(context.Matches.Where(m => m.CoachId == coach.CoachId).ToList());
                    }
                }
                Announcements = new ObservableCollection<Announcement>(context.Announcements.ToList());
            }

            AddPlayerCommand = new RelayCommand(_ => AddPlayer());
            DeletePlayerCommand = new RelayCommand(_ => DeletePlayer());
            UpdatePlayerCommand = new RelayCommand(_ => UpdatePlayer());
        }

        /// <summary>
        /// Adds a new player.
        /// </summary>
        private void AddPlayer()
        {
            var dialog = new AddPlayerDialog
            {
                DataContext = new Player()
            };
            if (dialog.ShowDialog() == true)
            {
                var newPlayer = (Player)dialog.DataContext;
                Players.Add(newPlayer);
                using (var context = new RugbyClubContext())
                {
                    context.Players.Add(newPlayer);
                    context.SaveChanges();
                }
            }
        }

        /// <summary>
        /// Deletes the selected player.
        /// </summary>
        private void DeletePlayer()
        {
            if (SelectedPlayer == null)
            {
                MessageBox.Show("Please select a player to delete.");
                return;
            }

            using (var context = new RugbyClubContext())
            {
                context.Players.Remove(SelectedPlayer);
                context.SaveChanges();
            }
            Players.Remove(SelectedPlayer);

        }

        /// <summary>
        /// Updates the selected player.
        /// </summary>
        private void UpdatePlayer()
        {
            if (SelectedPlayer == null)
            {
                MessageBox.Show("Please select a player to update.");
                return;
            }

            var dialog = new UpdatePlayerDialog
            {
                DataContext = SelectedPlayer
            };
            if (dialog.ShowDialog() == true)
            {
                using (var context = new RugbyClubContext())
                {
                    context.Players.Update(SelectedPlayer);
                    context.SaveChanges();
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
}
